import { Component, OnInit, Input, Output, EventEmitter } from '@angular/core';

declare var $: any;
@Component({
	selector: 'paginator',
	templateUrl: './paginator.component.html',
	styleUrls: ['./paginator.component.css']
})

export class PaginatorComponent implements OnInit {
	@Input() isJump:boolean = true;//是否显示跳转功能(默认显示)
	@Input() isPageCount:boolean = true;//是否显示页码信息(默认显示)
	@Input() isPageSize:boolean = true;//是否显示pageSize信息(默认显示)
	@Input() totalRecords: number;//总条数
	@Input() rows: number = 5;//page-size
	@Input() currentPage: number;//当前页码
	@Input() pageLinkSize: number;//显示的页码长度
	@Output() onPageChange = new EventEmitter();//发射事件
	private pageCount: number;//总页码
	private jumpPageNum:number = 1;//跳转页码
	private pageArr: Array < number > = [];//显示的页码长度
	private pageValidation: any = {
		isFirst: false,
		isLast: false
	};

	constructor() {}

	ngOnInit() {
//		this.initDefaultValue();
//		this.getPageCount();
//		this.getVisiblePageArr();
//		this.validateIfFirstLast();
	}
	ngOnChanges(){
		if(this.totalRecords>=0){
			this.initDefaultValue();
			this.getPageCount();
			this.getVisiblePageArr();
			this.validateIfFirstLast();
		}
	}
	//默认配置
	initDefaultValue() {
		this.rows = this.rows ? this.rows : 5;
		this.pageLinkSize = this.pageLinkSize ? this.pageLinkSize : 5;
		this.currentPage = this.currentPage ? this.currentPage : 0;
	}
	//页码数
	getPageCount() {
		this.pageCount = Math.ceil(this.totalRecords / this.rows);
	}
	//页码跳转
	jumpPageBlur(){
		if(this.jumpPageNum < 1){
			this.jumpPageNum = 1;
			this.currentPage = 0;
		}else if(this.jumpPageNum > this.pageCount){
			this.jumpPageNum = this.pageCount;
			this.currentPage = this.pageCount - 1;
		}else{
			this.currentPage = this.jumpPageNum - 1;
		}
		this.getVisiblePageArr();
		let data = {
			first: this.currentPage * this.rows,
			rows: Number(this.rows),
			page: this.currentPage,
			pageCount: this.pageCount
		}
		this.onPageChange.emit(data);
	}
	//页码点击事件
	changePage(actionKey: string,isClick:boolean) {
		if(isClick){return};
		this.getCurrentPage(actionKey);
		this.getVisiblePageArr();
		let data = {
			first: this.currentPage * this.rows,
			rows: Number(this.rows),
			page: this.currentPage,
			pageCount: this.pageCount
		}
		this.jumpPageNum = this.currentPage + 1;
		this.onPageChange.emit(data);
	}
	//分页pageSize回调事件
	updatePageSize(e){
		this.rows = parseInt(e);
		this.getCurrentPage('first');
		this.getPageCount();
		this.getVisiblePageArr();
		let data = {
			first: this.currentPage * this.rows,
			rows: Number(this.rows),
			page: this.currentPage,
			pageCount: this.pageCount
		}
		this.jumpPageNum = this.currentPage + 1;
		this.onPageChange.emit(data);
	}
	//页码显示处理
	getVisiblePageArr() {
		this.pageArr = [];
		let visiblePage = Math.min(this.pageLinkSize, this.pageCount);
		let start = Math.max(0, Math.ceil(this.currentPage - visiblePage / 2));
		// When page next to the end
		if(this.currentPage >= Math.floor(this.pageCount - visiblePage / 2)) {
			start = Math.max(0, this.pageCount - this.pageLinkSize);
		}
		let end = start + visiblePage - 1;
		for(var i = start; i <= end; i++) {
			this.pageArr.push(i);
		}
	}
	//样式处理
	getCurrentPage(actionKey: string) {
		if(actionKey == 'first') {
			this.currentPage = 0;
		} else if(actionKey == 'last') {
			this.currentPage = this.pageCount - 1;
		} else if(actionKey == 'pre') {
			if(this.currentPage <= 0) {
				return;
			}
			this.currentPage = this.currentPage - 1;
		} else if(actionKey == 'next') {
			if(this.currentPage >= this.pageCount - 1) {
				return;
			}
			this.currentPage = this.currentPage + 1;
		} else {
			this.currentPage = Number(actionKey);
		}

		this.validateIfFirstLast();
	}
	//校验
	validateIfFirstLast() {
		if(this.currentPage == 0) {
		    if(this.pageCount == 1){
		        this.pageValidation = {
                    isFirst: true,
                    isLast: true
                };
		    }else{
		        this.pageValidation = {
                    isFirst: true,
                    isLast: false
                };
		    }
			this.jumpPageNum = 1;
		} else if(this.currentPage == this.pageCount - 1) {
			this.pageValidation = {
				isFirst: false,
				isLast: true
			};
		} else {
			this.pageValidation = {
				isFirst: false,
				isLast: false
			};
		}
	}
}